<?php

class Reportes_BolsasController extends Zend_Controller_Action
{
    /**
     * 
     * @var Zend_Auth_Storage_Interface
     */
    private $_user_info;
    /**
     * 
     * @var Zwei_Admin_Acl
     */
    private $_acl;
    /**
     * 
     * @var string
     */
    private $_page;

    public function init()
    {
        $this->_helper->layout()->disableLayout();
        if (Zend_Auth::getInstance()->hasIdentity()) {
            $this->_user_info = Zend_Auth::getInstance()->getStorage()->read();
            $this->_acl = new Zwei_Admin_Acl($this->_user_info->user_name);
            $this->_page = "reportes/bolsas";
            if (!$this->_acl->isUserAllowed($this->_page)) exit("<p>Acceso denegado a $this->_page</p>"); 
        } else {
            $this->_redirect('index/login');
        }
    }

	public function indexAction()
	{
		// action body
	}

	public function activarAction()
	{

		if( $_REQUEST["unidad"] == "MIN" )
		{
			echo "<script  type='text/javascript'>
                alert('No es posible asignar la bolsa de minutos');
                window.parent.cargarPanelCentral('reportes/cons-online?consultar=Consultar&msisdn=".$_REQUEST["msisdn"]."');
              </script>";
			exit;
		}

		$xmlcontent = "<?xml version='1.0'  encoding='ISO-8859-1'?>";
		$xmlcontent .= "<msg>";
		$xmlcontent .= "<header id_trans='1111' app='xml' user='web' passw='web' action='1'/>";
		$xmlcontent .= "<req>";
		$xmlcontent .= "<op>req_pega_bolsa</op>";
		$xmlcontent .= "<msisdn>".$_REQUEST["msisdn"]."</msisdn>";
		$xmlcontent .= "<idPromo>".$_REQUEST["id_bolsa"]."</idPromo>";
		//$xmlcontent .= "<unidad>".$_REQUEST["unidad"]."</unidad>";
		$xmlcontent .= "</req>";
		$xmlcontent .= "</msg>";

		global $urlServerXML;
		$url = $urlServerXML;

		//echo $url;
		sleep(1);

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch, CURLOPT_VERBOSE, 0);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_POST, 1);
		//curl_setopt($ch, CURLOPT_PUT, 1);
		curl_setopt($ch, CURLOPT_TIMEOUT, 10);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlcontent);

		$result = curl_exec($ch);
		//echo "Llega: [".$result."]<br>";

		if (curl_errno($ch))
		{
			curl_close($ch);
			echo "<script  type='text/javascript'>
                alert('No se pudo asignar la bolsa.');
                window.parent.cargarPanelCentral('reportes/cons-online?consultar=Consultar&msisdn=".$_REQUEST["msisdn"]."&norepost=1');
              </script>";
		}
		else
		{
			curl_close($ch);
			if( procesaXML($result) )
			{
				echo "<script  type='text/javascript'>
                alert('La bolsa fue asignada con exito.');
                window.parent.cargarPanelCentral('reportes/cons-online?consultar=Consultar&msisdn=".$_REQUEST["msisdn"]."&norepost=1');
              </script>";
			}
			else
			echo "<script  type='text/javascript'>
                alert('No se pudo asignar la bolsa.');
                window.parent.cargarPanelCentral('reportes/cons-online?consultar=Consultar&msisdn=".$_REQUEST["msisdn"]."&norepost=1');
              </script>";

		}
			
			
	}

	function procesaXML($result)
	{
		include_once ROOT_DIR."/classes/class_xml.php";
		$xml = new SimpleXMLElementExtended($result);
		if( !is_object($xml) )
		{
			echo "Error XML";
			return false;
		}

		//    echo "RESULT:  ".(string)$xml->resp->result;
		//    echo "STRRESULT:  ".(string)$xml->resp->strresult;

		if( (string)$xml->resp->result == 0 )
		return true;
		else
		return false;
	}

}



